#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    _videoPlayer = new VideoPlayer(this);
    connect(_videoPlayer,&VideoPlayer::stateChange,this,&MainWindow::videoPlayer_change_state);
    connect(_videoPlayer,&VideoPlayer::initFinish,this,&MainWindow::videoPlayer_init_finish);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_stopButton_clicked()
{

//    ui->stackedWidget->setCurrentIndex(1);

    _videoPlayer->stop();
    ui->playerWidget->setCurrentWidget(ui->selectFilePage);

}

void MainWindow::on_openFileBtn_clicked()
{

    QString file_path = QFileDialog::getOpenFileName(this,
                                                       tr("文件对话框！"),
                                                       "/Users/tgy/Documents/ffmpeg/video",
                                                       tr("video files(*.avi *.mp4 *.wmv *.flv)"));
    if(file_path.isEmpty())
    {

        return;
    }
    qDebug() << file_path;

    _videoPlayer->setVideoPath(file_path.toUtf8().data());
    _videoPlayer->play();

    ui->playerWidget->setCurrentWidget(ui->playContentPage);

}

void MainWindow::on_playSlider_valueChanged(int value)
{


    qDebug() << value;
}

void MainWindow::on_voiceSlider_valueChanged(int value)
{
    ui->showVoiceLabel->setText(QString("%1").arg(value));
}

void MainWindow::videoPlayer_change_state(VideoPlayer *videoPlayer)
{

    qDebug() << videoPlayer->getState();

    if(videoPlayer->getState() == VideoPlayer::Stop) {

        ui->playButton->setText("播放");
        ui->playButton->setEnabled(false);
        ui->stopButton->setEnabled(false);
        ui->playSlider->setEnabled(false);
        ui->allTimeLabel->setEnabled(false);
        ui->currentTimeLabel->setEnabled(false);
        ui->voiceSlider->setEnabled(false);
        ui->showVoiceLabel->setEnabled(false);
        ui->silenceButton->setEnabled(false);

    }else if(videoPlayer->getState() == VideoPlayer::Pause) {

        ui->playButton->setText("播放");

    }else if(videoPlayer->getState() == VideoPlayer::Playing) {

        ui->playButton->setText("暂停");
        ui->playButton->setEnabled(true);
        ui->stopButton->setEnabled(true);
        ui->playSlider->setEnabled(true);
        ui->allTimeLabel->setEnabled(true);
        ui->currentTimeLabel->setEnabled(true);
        ui->voiceSlider->setEnabled(true);
        ui->showVoiceLabel->setEnabled(true);
        ui->silenceButton->setEnabled(true);
    }

}

void MainWindow::videoPlayer_init_finish(VideoPlayer *videoPlayer)
{

    qDebug() << "duration" << videoPlayer->getDuration();
}

